package leetcode.code1814;

import java.util.HashMap;
import java.util.Map;

public class Solution extends Solution1814 {

	private int E = (int) 1e9 + 7;

	@Override
	public int countNicePairs(int[] nums) {
		Map<Integer, Integer> map = new HashMap<>();
		Map<Integer, Integer> hash = new HashMap<>();
		int ans = 0;
		for (int num : nums) {
			int sub = num - this.reverse(num, hash);
			int pairs = map.getOrDefault(sub, 0);
			ans = (ans + E + pairs) % E;
			map.put(sub, pairs + 1);
		}
		return ans;
	}

	public int reverse(int num, Map<Integer, Integer> hash) {
		if(hash.containsKey(num)) {
			return hash.get(num);
		}
		int ans = 0;
		while (num > 0) {
			int mod = num % 10;
			ans = ans * 10 + mod;
			num /= 10;
		}
		hash.put(num, ans);
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
